package cn.sccl.springmvc.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import cn.sccl.springmvc.exception.MessageException;
import cn.sccl.springmvc.pojo.Items;
import cn.sccl.springmvc.pojo.QueryVo;
import cn.sccl.springmvc.service.ItemService;

/**
 * @author 饶爽
 * @time 2019年10月20日 上午7:04:37
 * @version 1.0.0
 * 商品管理
*/
@Controller
public class ImemController {
    
    @Autowired
    private ItemService itemService;
    //入门程序 第一   包类 + 类包 + 方法名
    /**
     * 1.ModelAndView  无敌的，    带着数据，返回视图路径，但不建议使用（不符解耦思想）
     * 2.String    返回视图路径     model带数据，官方推荐此种方式，解耦  -- 数据、视图、分离 ， MVC（建议使用 ） 
     * 3.void       ajax请求   合适   json格式数据 （response异步请求使用
     * @throws MessageException 
     */
    @RequestMapping(value = "/item/itemlist.action")
//    public ModelAndView itemList() {//视图方案
    public String itemList(Model model,HttpServletRequest request,HttpServletResponse response) throws MessageException {    
        
        //获取数据库中的数据
        List<Items> list = itemService.selectItemsList();
        
        /*if (null == null) {
            throw new MessageException("商品信息不能为空！");
        }*/
        /*ModelAndView mav = new ModelAndView();
        //添加数据
        mav.addObject("itemList", list);*/
        model.addAttribute("itemList", list);
//        mav.setViewName("/WEB-INF/jsp/itemList.jsp");
        //视图解析器根据前缀+逻辑视图名+后缀拼接出来物理路径
        /*mav.setViewName("itemList");//配置视图解释器后的写法
        return mav; */
        return "itemList";
    }
    //跳转修改商品信息界面
    @RequestMapping(value = "/itemEdit.action")
    //自定义参数绑定写法（value指定前端参数名，required指定参数是否必须，defaultValue指定默认分页页码）
//    public ModelAndView toEdit(@RequestParam(value = "id",required = false,defaultValue = "1") Integer idaaq,
    
    public ModelAndView toEdit(Integer id,HttpServletRequest request,HttpServletResponse response
            ,HttpSession session,Model model){
        //Servlet时代开发
//        String id = request.getParameter("id");//默认参数绑定
        
        //查询一个商品
//        Items items = itemService.selectItemsById(Integer.parseInt(id));
        Items items = itemService.selectItemsById(id);//简单参数绑定
        ModelAndView mav = new ModelAndView();
        //数据
        mav.addObject("item", items);
        mav.setViewName("editItem");
        return mav;
    }
    //提交修改页面 入参  为 Items对象  
    @RequestMapping(value = "/updateitem.action")
//    public ModelAndView updateitem(Items items){
//    public ModelAndView updateitem(QueryVo vo){
    public String updateitem(QueryVo vo,MultipartFile pictureFile) throws Exception{
        //保存图片到 
        String name = UUID.randomUUID().toString().replaceAll("-", "");
        //
        String ext = FilenameUtils.getExtension(pictureFile.getOriginalFilename());
        
        pictureFile.transferTo(new File("C:\\Program Files (x86)\\images\\" + name + "." + ext));
        
        vo.getItems().setPic(name + "." + ext);
        //修改
//        itemService.updateItemsById(items);
        itemService.updateItemsById(vo.getItems());
        
//        ModelAndView mav = new ModelAndView();
//        mav.setViewName("success");
//        return "forward:/item/itemlist.action";
        return "redirect:/itemEdit.action?id="+vo.getItems().getId();
    }
    //删除多个
    @RequestMapping(value = "/deletes.action")
//    public ModelAndView deletes(Integer[] ids){//数组
    public ModelAndView deletes(QueryVo vo){//    
        
//        itemService.deleteall(ids);
        itemService.deleteallQueryVo(vo);
        ModelAndView mav = new ModelAndView();
        mav.setViewName("success");
        return mav;
    }
    //修改
    @RequestMapping(value = "/updates.action",method = {RequestMethod.POST,RequestMethod.GET})
    public ModelAndView updates(QueryVo vo){
        //修改逻辑略。。
        
        ModelAndView mav = new ModelAndView();
        mav.setViewName("success");
        return mav;
    }
    //json数据交互
    @RequestMapping(value = "/json.action")
    public @ResponseBody Items json(@RequestBody Items items){
        System.out.println(items);
        return items;
    }
    //RestFul风格的开发
    @RequestMapping(value = "/itemEdit/{id}.action")
    public ModelAndView toEdit1(@PathVariable Integer id,
            HttpServletRequest request,HttpServletResponse response
            ,HttpSession session,Model model){
        //Servlet时代开发
//      String id = request.getParameter("id");       
        //查询一个商品
//      Items items = itemService.selectItemsById(Integer.parseInt(id));
        
        Items items = itemService.selectItemsById(id);
        ModelAndView mav = new ModelAndView();
        //数据
        mav.addObject("item", items);
        mav.setViewName("editItem");
        return mav; 
    }
    //去登陆的页面
    @RequestMapping(value = "/login.action",method = RequestMethod.GET)
    public String login(){
        return "login";
    }
    @RequestMapping(value = "/login.action",method = RequestMethod.POST)
    public String login(String username,HttpSession httpSession){
        httpSession.setAttribute("USER_SESSION", username);
        return "redirect:/item/itemlist.action";
    }
    
}
